package com.pro.h5case.wap.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.h5case.common.controller.BaseController;
import com.pro.h5case.common.model.BDictionary;
import com.pro.h5case.common.model.BPlan;
import com.pro.h5case.common.service.BDictionaryService;
import com.pro.h5case.common.service.BPlanService;
import com.pro.h5case.wap.service.IndexService;

@Controller
@Scope("prototype")
@RequestMapping(value = "/wap")
public class WapIndexController extends BaseController {

	@Autowired
	BDictionaryService bDictionaryService;
	@Autowired
	BPlanService bPlanService;
	
	//首页进入的控制器
	@RequestMapping(value = "/index.html", method = RequestMethod.GET)
	public String index(Model m,String name) {
		
		//行业
		BDictionary bDictionary = new BDictionary();
		bDictionary.setDicType("HANGYE");
		bDictionary.setDicDeleteState((byte)1);
		List<BDictionary> industries = bDictionaryService.selectByPojo(bDictionary);
		m.addAttribute("industies", industries);
		//节日
		bDictionary.setDicType("JIERI");
		List<BDictionary> holidays = bDictionaryService.selectByPojo(bDictionary);
		m.addAttribute("holidays", holidays);
		//功能
		bDictionary.setDicType("GONGNENG");
		List<BDictionary> functions = bDictionaryService.selectByPojo(bDictionary);
		m.addAttribute("functions", functions);
		String title = null;
		if (name!=null&&!name.equals(' ')) {
			try {
				title = new String(name.getBytes("ISO8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		m.addAttribute("name", title);
		return "wap/index";
	}
	
	@RequestMapping("/cases.html")
	public @ResponseBody Map<String,Object> getBPlan(HttpServletRequest request,HttpServletResponse response,String planName,String planTag,Integer pageNo,Integer pageSize,String orderBy){
		
		//查询的条件
		Map<String ,Object> condition = new HashMap<String,Object>();
		try {if (planName!=null) {
			planName = new String(planName.getBytes("ISO8859-1"),"UTF-8");
		}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		condition.put("planName", planName);
		//condition.put("planTag",planTag);
		String[] planT = planTag.split(",");
		for (int i = 0; i < planT.length; i++) {
			if ("undefined".equals(planT[i])) {
				planT[i]=null;
			}
			condition.put("planTag"+(i+1), planT[i]);
			System.out.println("planTag"+(i+1)+"——"+planT[i]);
		}
		condition.put("planAudit","已审核");
		condition.put("planDeleteState", 1);
		String order = "plan_create_time desc";
		if ("new".equals(orderBy)){
			order = "plan_create_time desc";
		}else if("hot".equals(orderBy)){
			order = "plan_hot desc";
		}
		List<BPlan> list = bPlanService.selectByCondition(condition,pageNo,pageSize,order);
		int count = bPlanService.selectCountByCondition(condition);
		BDictionary bDictionary = new BDictionary();
		bDictionary.setDicDeleteState((byte)1);
		List<BDictionary> hangyeList = bDictionaryService.selectByPojo(bDictionary);
		for (int i = 0; i < list.size(); i++) {
			String [] tags = list.get(i).getPlanTag().split(",");
			System.out.println(tags);
			String varTag="";
			if(tags.length>0){
				Integer tagSize = 0;
				if (tags.length<3) {
					tagSize = tags.length;
				}else{
					tagSize = 3;
				}
				for (int j = 0; j < tagSize; j++) {//tags.length
					for (int k = 0; k < hangyeList.size(); k++) {//hangyeList.size()
						String hangye = hangyeList.get(k).getDicCode();
						if(hangye.equals(tags[j])){
							varTag +=hangyeList.get(k).getDicName() + ",";
						}
					}
				}
			}
			list.get(i).setPlanTag(varTag);
		}
		System.out.println(list.size());
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("list", list);
		map.put("count", count);
		return map;
	}

}
